package client

import (
	amount1RPC "amount1_server/api/Amount1Server/v1"
	"context"
	"time"

	"github.com/go-kratos/kratos/contrib/registry/etcd/v2"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
)

func NewAmount1Client(registry *etcd.Registry, logger log.Logger) (amount1RPC.Amount1ServiceClient, error) {
	conn, err := grpc.DialInsecure(
		context.Background(),
		grpc.WithEndpoint("discovery:///amount1-server"), // amount1-server的service名称，注意是三个下划线！
		//grpc.WithEndpoint("127.0.0.1:9001"), // 使用amount1_server部署的地址也能找到～注意只写地址即可
		grpc.WithTimeout(15*time.Second),
		grpc.WithDiscovery(registry),
		grpc.WithMiddleware(
			tracing.Client(),
			recovery.Recovery(),
		),
	)
	if err != nil {
		log.NewHelper(logger).WithContext(context.Background()).Errorw("kind", "grpc-client", "reason", "GRPC_CLIENT_INIT_ERROR", "err", err)
		return nil, err
	}
	return amount1RPC.NewAmount1ServiceClient(conn), nil
}
